package cn.jiahua.hospital.utils;

import cn.jiahua.hospital.config.OssConfig;
import com.aliyun.oss.ClientException;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.OSSException;
import com.aliyun.oss.model.OSSObject;
import com.aliyun.oss.model.PutObjectResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;

@Component
public class OssUtils {

    @Autowired
    private OssConfig ossConfig;

    //1、获取ossClient(oss客户端)
    public OSS getOssClient() {
        OSS oss = new OSSClientBuilder().build(ossConfig.getEndPoint(), ossConfig.getAccesskeyid(), ossConfig.getAccesskeysecret());
        return oss;
    }

    //2、上传文件
    public String uploadByStream(File file, String fileName) {
        OSS ossClient = null;
        ossClient = getOssClient();
        String strResult = null;
        try {
            InputStream inputStream = new FileInputStream(file);
            PutObjectResult putObjectResult = ossClient.putObject(ossConfig.getBucketName(), fileName, inputStream);
            strResult = putObjectResult.getETag();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (Exception e) {
            if (ossClient != null) {
                ossClient.shutdown();
                ;
            }
        } finally {
            ossClient.shutdown();
        }
        return strResult;
    }

    //使用仓库名和需要删除的文件的路径进行删除
    public boolean delFile(String bucketName, String path) {
        OSS ossClient = null;
        try {
            ossClient = getOssClient();
            ossClient.deleteObject(bucketName, path);
        } catch (OSSException e) {
            e.printStackTrace();
            return false;
        } catch (ClientException e) {
            e.printStackTrace();
            return false;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        } finally {
            ossClient.shutdown();
        }
        return true;
    }

    //提供仓库名和文件路径进行下载
    public void downloadFile(String bucketName, String path, HttpServletResponse response) throws IOException {
        OSS ossClient = getOssClient();
        OSSObject ossObject = ossClient.getObject(bucketName, path);
        InputStream dataInputStream = ossObject.getObjectContent();

        ServletOutputStream dataOutputStream = response.getOutputStream();
        //获取文件名
        String fileName = path.substring(path.lastIndexOf("/") + 1);

        //response添加头信息，如下：用于设置下载文件
        response.addHeader("Content-Disposition", "attachment;filename=" + fileName);
        //下载图片是需要注意设置contentType，否则会出现图片失真
        response.setContentType("application/octet-stream");

        byte[] car = new byte[1024];
        int L = 0;
        while ((L = dataInputStream.read(car)) != -1) {
            dataOutputStream.write(car, 0, L);
        }
        dataOutputStream.flush();

        if (dataOutputStream != null) {
            dataOutputStream.close();
        }

        if (dataInputStream != null) {
            dataInputStream.close();
        }
        if (ossObject != null)
            ossObject.close();
        if (ossClient != null)
            ossClient.shutdown();
    }

}
